﻿@model Admin.CYT369.Com.DAL.Domains.Role
@{
    Layout = null;
    var roleId = 0;
    bool isInternalRole = false;

    if (Model != null)
    {
        roleId = Model.Id;
        isInternalRole = Model.IsInternal;
    }

    var tableName = PageViewModel.PageTableID(this, "edit_role_user");
    var tableToolBar = PageViewModel.PageTableToolbar(this, "edit_role_user");

    var addRoleUserTableName = PageViewModel.PageFunctionName(this, "add_role_user_table");
    var addRoleUserFunName = PageViewModel.PageFunctionName(this, "add_role_user");

    var removeRoleUserFunName = PageViewModel.PageFunctionName(this, "remove_role_user");
    var addRoleUserDlgName = PageViewModel.ControlID(this, "add_user_dlg");
    var saveRoleUserFunName = PageViewModel.PageFunctionName(this, "save_role_user");
    var deleteFunName = PageViewModel.PageFunctionName(this, "delete_fun");
}

<script type="text/javascript">
    function @addRoleUserFunName (id) {
        var dlg = $dlg('@addRoleUserDlgName');
        dlg.dialog({
            title: '选择成员', width: 800, height: 500,
            closed: false, cache: false, modal: true, maximizable: false, resizable: true,
            href: '~/Systems/_AddRoleUser?tbname=@addRoleUserTableName&ID=' + id,
            onResize: function () { $(this).dialog('center'); },
            buttons: [
                {
                    text: "确 定",
                    handler: function () { @saveRoleUserFunName (); },
                    iconCls: "icon-save"
                }, {
                    text: "取 消",
                    handler: function () {
                        $("#@addRoleUserDlgName").dialog("close");
                    },
                    iconCls: "icon-cancel"
                }
            ]
        });
    }

    function @saveRoleUserFunName () {
        var dlg = $dlg('@addRoleUserDlgName');
        var grid = dlg.find("#@addRoleUserTableName");
        var rows = grid.datagrid("getChecked");
        if (rows.length > 0) {
            var roleId = dlg.find("#_AddRoleUser_ID").val();
            var list = new Array();
            for (var i = 0; i < rows.length; i++) {
                list.push({
                    RoleId: parseInt(roleId),
                    UserId: rows[i].Id
                });
            }
            $.ajax({
                url: "~/Systems/SaveRoleUser",
                type: "POST",
                contentType: "application/json",
                data: JSON.stringify(list),
                success: function (e) {
                    if (e != null) {
                        if (e.Result) {
                            $('#@tableName').datagrid('reload');
                            $('#@addRoleUserDlgName').dialog('close');
                        } else {
                            $.messager.alert('保存失败', e.Msg, 'error');
                        }
                    }
                },
                error: function (e) {
                    ShowAjaxError(e);
                }
            });
        }
    }

    function @deleteFunName (id) {
        if (id > 0 && confirm("确认删除该成员?")) {
            $.ajax({
                async: false,
                method: "DELETE",
                url: $.Content("~/Systems/DeleteRoleUser/" + id),
                context: $("#@tableName"),
                success: function (e) {
                    if (e.Result) {
                        this.datagrid('reload');
                    } else {
                        $.messager.alert('删除数据', '删除失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e1, e2, e3) {
                    ShowAjaxError(e1, "删除数据");
                    this.datagrid('reload');
                }
            });
        }
    }

    function @removeRoleUserFunName () {
        var rows = $("#@tableName").datagrid("getChecked");
        if (rows.length > 0 && confirm("确认移除选中的成员?")) {
            var list = new Array();
            for (var i = 0; i < rows.length; i++) {
                list.push(rows[i].Id);
            }
            $.ajax({
                async: false,
                method: "DELETE",
                url: $.Content("~/Systems/DeleteRoleUserList"),
                data: JSON.stringify(list),
                context: $("#@tableName"),
                success: function (e) {
                    if (e.Result) {
                        this.datagrid('reload');
                    } else {
                        $.messager.alert('删除数据', '删除失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e1, e2, e3) {
                    ShowAjaxError(e1, "删除数据");
                    this.datagrid('reload');
                }
            });
        }
    }
</script>

<table id="@tableName" class="easyui-datagrid" style="width:100%;" rownumbers="true" fit="true" fitColumns="true"
       pagination="true" pageSize="@SystemSettingService.Instance.PageSize" pageList="@SystemSettingService.Instance.PageList"
       data-options="striped:true,border:false,singleSelect:false,url:'~/Systems/GetRoleUsersByRoleID/@roleId',method:'get',toolbar:'#@tableToolBar'">
    <thead>
        <tr>
            <th data-options="field:'_',width:30,align:'center',checkbox:true"></th>
            <th data-options="field:'Id',width:60,align:'center',formatter:function(v,r,i){
                    var userInfo = GetObject(r.UserId, '~/Systems/GetUserByID');
                    if(userInfo != null) {
                        var u = userInfo.User;
                        if(u!=null){
                            r.FullName = u.FullName;
                            r.UserName = u.UserName;
                            r.PhoneNumber = u.PhoneNumber;
                            r.Email = u.Email;
                            r.IsInternal = u.IsInternal;
                        }
                    }
                    if(!(r.IsInternal==true && @isInternalRole.ToString().ToLower())){ return GridRowDeleteFmt('@deleteFunName', r.Id); }
                }">移除</th>

            <th data-options="field:'FullName',width:120">姓名</th>
            <th data-options="field:'UserName',width:120">登录账户</th>
            <th data-options="field:'PhoneNumber',width:120">手机号</th>
            <th data-options="field:'Email',width:120">Email</th>
        </tr>
    </thead>
</table>
<div id="@tableToolBar" class="content_toolbar">
    <a href="javascript:@string.Format("{0}({1})", addRoleUserFunName, roleId)" class="easyui-linkbutton" data-options="iconCls:'icon-add'" style="margin:5px;">添加</a>
    <a href="javascript:@removeRoleUserFunName ()" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" style="margin:5px;">移除</a>
</div>